//
//  ScrollViewViewController.m
//  ScrollView
//
//  Created by Neelam Roy on 4/8/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "ScrollViewViewController.h"

@implementation ScrollViewViewController
@synthesize mscrollview;
@synthesize Ten;
@synthesize button;
@synthesize Duoi;
@synthesize lableText;
@synthesize Infor;
@synthesize webView;
//#define HTTP @"http://checkdomain.matbao.vn/?domain="//http://checkdomain.matbao.vn/?domain=matbawwwo.vn&Type=1
//#define TYPE @"&Type=1"
//#define TYPE_InFor @"&Type=3"
// To load the Scroll View

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.tabBarItem.image = [UIImage imageNamed:@"bar_info.png"];
        
           
        //self.view.backgroundColor=[UIColor blackColor];
        
        Infor.textAlignment=UITextAlignmentCenter;       
        lableText.userInteractionEnabled = YES;
        [Duoi setHidden:TRUE];
        UITapGestureRecognizer *tapGesture =[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)] autorelease];
        [lableText addGestureRecognizer:tapGesture];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    mscrollview.frame = CGRectMake(0, 0, 320, 460);
    mscrollview.contentSize = CGSizeMake(320, 800-200);
    [mscrollview setBackgroundColor:[UIColor clearColor]];
    if (IS_IPHONE5) {
        
    }
    else
    {
        
    }
    
}


- (void)viewWillAppear:(BOOL)animated {
   	[super viewWillAppear:animated];
	
	
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}
-(void)labelTap
{
    NSLog(@"lable text");
    [Duoi setHidden:FALSE];
}

- (void)dealloc {
    [arrayTag release];
	[mscrollview release];
    [super dealloc];
}
#pragma mark -Action
-(IBAction) submit:(id) sender
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    [Duoi setHidden:TRUE];
    if([Ten.text isEqualToString:@""])
    {
        //Ten.text=@"";
        // alert view
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:nil
                              message: @"Vui Lòng Điền Tên Cần Tra Cứu"
                              delegate: nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        
    }
    else{
        [self addRequestbyString:[NSString stringWithFormat:@"%@%@",Ten.text,lableText.text] type:TYPE];
        activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        [activity setFrame:CGRectMake(0, 0, 320, 480)];
        [activity startAnimating];
        [self.view addSubview:activity];
        [activity setBackgroundColor:[UIColor blackColor]];
        [activity setAlpha:0.6];
        
        [activity release];
        
        

    }
}
#pragma mark -Method
-(void)addRequestbyString:(NSString*)str type:(NSString*)Type
{
    NSString * searching=[NSString stringWithFormat:@"%@%@%@",HTTP,str,Type];
   // NSLog(@"Searching =%@ ",searching);
    
    NSURL *treeURL =[NSURL URLWithString:[searching stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    ASIHTTPRequest * request= [ASIHTTPRequest requestWithURL:treeURL];
    [request setDelegate:self];
    [request startAsynchronous];
    
    self.view.userInteractionEnabled=NO;//disable
}
#pragma mark -Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
	//NSString *temp=[NSString stringWithFormat:@"%@",textField.text];
	
	
	return TRUE;
    
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
   // NSLog(@"clear text");
		
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
}
-(void)addInforOfDomain:(NSString*)strWebsiteUlr type:(NSString*)Type
{
    
     NSString * searching=[NSString stringWithFormat:@"%@%@%@",HTTP,strWebsiteUlr,Type];
    
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:searching];
    
    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    
    //Load the request in the UIWebView.
    [webView loadRequest:requestObj];
}
-(void)requestFinished:(ASIHTTPRequest *)arequest
{
    
    
    NSString *responseString = [arequest responseString];
   // NSLog(@"responseString %@",responseString);
    //faill is chua cap
    //true cap roi
    
    if([responseString isEqualToString:@"True"])
    {
        /// is Resiter
       
        Infor.text=[NSString stringWithFormat:@"Tên Miền %@%@ đã đăng kí",Ten.text,lableText.text];
        [self addInforOfDomain:[NSString stringWithFormat:@"%@%@",Ten.text,lableText.text] type:TYPE_InFor];
    }
    else if([responseString isEqualToString:@"False"])
    {
        [activity stopAnimating];
        [activity removeFromSuperview];
        activity = nil;
        // no
        self.view.userInteractionEnabled=YES;//enable
        Infor.text=[NSString stringWithFormat:@"Tên Miền %@%@ chưa đăng kí",Ten.text,lableText.text];
        [self.webView loadHTMLString:@"" baseURL:[NSURL URLWithString:@"http://google.com"]];
    }
        
    
     
    
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
    [activity stopAnimating];
    [activity removeFromSuperview];
    activity = nil;
    NSLog(@"%@",[request error]);
     self.view.userInteractionEnabled=YES;//enable
    
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:nil
                          message: @"Vui Lòng Kiểm tra Internet"
                          delegate: nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    
}
#pragma mark -
#pragma mark Picker View Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
	
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
	
	return [arrayTag count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	
    
	return [arrayTag objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	
	NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayTag objectAtIndex:row], row);
    lableText.text=[arrayTag objectAtIndex:row];
   
}
#pragma mark - WebView
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activity stopAnimating];
    [activity removeFromSuperview];
    activity = nil;
    self.view.userInteractionEnabled=YES;//enable
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;//(interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        
        [self changeTheViewToPortrait:YES andDuration:duration];
        
    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        
        [self changeTheViewToPortrait:NO andDuration:duration];
    }
}
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    
    if(portrait){
        //change the view and subview frames for the portrait view
          mscrollview.contentSize = CGSizeMake(320, 500-80);
        //mscrollviewPortrait.contentSize = CGSizeMake(320, 500-180);
       //[self setpositionLableLandScape:FALSE];
        
    }
    else{   
        mscrollview.contentSize = CGSizeMake(320, 500-180);
        //change the view and subview  frames for the landscape view
       // mscrollviewPortrait.contentSize = CGSizeMake(320, 500-180);
       // [self setpositionLableLandScape:TRUE];
        
    }
    
    [UIView commitAnimations];
}

@end
